package com.yunji.base.business;

import com.yunji.base.convert.TextRecordConvert;
import com.yunji.base.service.TextRecordService;
import com.yunji.common.enums.common.TextRecordType;
import com.yunji.common.model.dto.TextRecordDTO;
import com.yunji.common.model.vo.TextRecordVO;
import com.yunji.common.utils.bean.BeanUtils;
import io.jsonwebtoken.lang.Assert;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class TextRecordBusiness {

    @Resource
    private TextRecordService textRecordService;

    public Long create(TextRecordDTO dto) {
        checkTextType(dto);
        return textRecordService.create(TextRecordConvert.DTOToDO(dto));
    }

    public TextRecordVO getInfo(Long id) {
        return textRecordService.getInfo(id);
    }

    public void edit(TextRecordDTO dto) {
        checkTextType(dto);
        textRecordService.update(TextRecordConvert.DTOToDO(dto));
    }

    public List<TextRecordVO> list(TextRecordDTO dto) {
        return textRecordService.getPageList(dto);
    }

    public void delete(Long id) {
        textRecordService.delete(id);
    }

    public String getPrivacyText(Integer textType) {
        // 待商榷 先返回第一个
        TextRecordDTO dto = new TextRecordDTO();
        dto.setTextType(textType);
        List<TextRecordVO> list = textRecordService.getPageList(dto);
        return BeanUtils.isNotNull(list) ? list.get(0).getTextContent() : "";
    }

    public void checkTextType(TextRecordDTO dto) {
        if (BeanUtils.equals(dto.getTextType(), TextRecordType.PRIVACY.getCode(),TextRecordType.POLICY.getCode())) {
            Assert.isTrue(!textRecordService.checkUniqueTextType(dto.getTextType(), dto.getTextId()),"该类型只能存在一个");
        }
    }
}
